﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YJKArchUtils.Utils;
using YJKRevitRampModule.Utils;

namespace YJKRevitRampModule.CreateRampByDoor
{
	public partial class PassagewayRampForm : System.Windows.Forms.Form
	{
		public PassagewayRampForm(DoorGeometryExtractor extractor)
		{
			this.InitializeComponent();
			this.m_Extractor = extractor;
			this.m_WallThickness = 0.656167979;
			int num = this.tbLength.Height / 5;
			this.m_Previewer = new Previewer(this, num, this.tbLength.Top, this.label1.Left - num * 2, this.rbPerson.Bottom - this.tbLength.Top, 1u);
			base.MouseWheel += this.OnMouseWheel;
			this.m_Previewer.PreviewerControl().MouseDoubleClick += this.OnMouseDoubleClick;
			this.InitTransform();
		}

		private void InitTransform()
		{
			Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisX, -1.2566370614359172, XYZ.Zero);
			Transform transform2 = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisY, -0.3490658503988659, XYZ.Zero);
			Transform transform3 = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, -0.11635528346628864, XYZ.Zero);
			this.m_Transform = transform3.Multiply(transform2.Multiply(transform));
		}

		public double RampLength
		{
			get
			{
				double result;
				try
				{
					result = Common.MMToFeet(Convert.ToDouble(this.m_Length));
				}
				catch
				{
					result = 2000.0;
				}
				return result;
			}
		}

		public double RampOffsetWidth
		{
			get
			{
				double result;
				try
				{
					result = Common.MMToFeet(Convert.ToDouble(this.m_OffsetWidth));
				}
				catch
				{
					result = 50.0;
				}
				return result;
			}
		}

		public double RampBottomOffsetWidth
		{
			get
			{
				double result;
				try
				{
					result = Common.MMToFeet(Convert.ToDouble(this.m_BottomOffsetWidth));
				}
				catch
				{
					result = 1000.0;
				}
				return result;
			}
		}

		public RampType TheRampType
		{
			get
			{
				if (!this.rbPerson.Checked)
				{
					return RampType.RT_Car;
				}
				return RampType.RT_Person;
			}
		}

		public double RampVOffset
		{
			get
			{
				double result;
				try
				{
					result = -Common.MMToFeet(Convert.ToDouble(this.m_VOffset));
				}
				catch
				{
					result = 0.0;
				}
				return result;
			}
		}

		public double RampHOffset
		{
			get
			{
				double result;
				try
				{
					result = Common.MMToFeet(Convert.ToDouble(this.m_HOffset));
				}
				catch
				{
					result = 0.0;
				}
				return result;
			}
		}

		public double RampHeight
		{
			get
			{
				double result;
				try
				{
					result = Common.MMToFeet(Convert.ToDouble(this.m_Height));
				}
				catch
				{
					result = 500.0;
				}
				return result;
			}
			set
			{
				this.tbHeight.Text = value.ToString("f0");
			}
		}

		public SkidResistanceType RampSkidResistanceType
		{
			get
			{
				if (this.cmbSkidResistanceType.SelectedIndex == 0)
				{
					return SkidResistanceType.SRT_None;
				}
				if (this.cmbSkidResistanceType.SelectedIndex == 1)
				{
					return SkidResistanceType.SRT_Protrude;
				}
				return SkidResistanceType.SRT_Concave;
			}
		}

		private void PassagewayRampForm_Load(object sender, EventArgs e)
		{
			this.m_Initialized = false;
			this.lbTip1.Visible = false;
			this.lbTip2.Visible = false;
			this.ReadData();
			this.GetDoorInfos();
			this.m_Initialized = true;
		}

		private void PassagewayRampForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			this.SaveData();
		}

		protected void OnMouseWheel(object sender, MouseEventArgs e)
		{
			if (this.m_Previewer.Focused)
			{
				double angle = (double)e.Delta / 180.0 * Math.PI / 20.0;
				Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, angle, XYZ.Zero);
				this.m_Transform = this.m_Transform.Multiply(transform);
				this.Refresh();
			}
		}

		protected void OnMouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.InitTransform();
			this.Refresh();
			this.m_Previewer.ZoomE();
		}

		private void GetDoorInfos()
		{
			this.m_DoorFaces.Clear();
			this.m_DoorFacesIsGlass.Clear();
			this.m_Extractor.GetDefaultDoorGeometryInfo(ref this.m_DoorFaces, ref this.m_DoorFacesIsGlass);
		}

		private void Redraw()
		{
			this.m_Previewer.Transparent(false);
			this.m_Plotter = new RampPlotter();
			double num = 750.0;
			try
			{
				double rampLength = Common.FeetToMM(this.RampLength);
				double rampHeight = Common.FeetToMM(this.RampHeight);
				double rampVOffset = Common.FeetToMM(this.RampVOffset);
				double rampHOffset = Common.FeetToMM(this.RampHOffset);
				double num2 = Common.FeetToMM(this.RampOffsetWidth);
				double num3 = Common.FeetToMM(this.RampBottomOffsetWidth);
				double rampWidth = num + num2 * 2.0;
				double rampBottomWidth = num + num3 * 2.0;
				this.m_Plotter.Draw(this.TheRampType, this.m_Transform, rampLength, rampHeight, rampWidth, rampBottomWidth, rampVOffset, rampHOffset, this.RampSkidResistanceType, this.m_DoorFaces, this.m_DoorFacesIsGlass, this.m_WallThickness);
			}
			catch (Exception)
			{
			}
		}

		protected override void DefWndProc(ref Message m)
		{
			switch (m.Msg)
			{
			case 1125:
				if (m.WParam.ToInt32() == 1 && this.m_Initialized)
				{
					this.Redraw();
					this.m_Previewer.Curves = this.m_Plotter.Curves;
					this.m_Previewer.TextNotes = this.m_Plotter.Texts;
					this.m_Previewer.FillRegions = this.m_Plotter.FillRegions;
					return;
				}
				break;
			case 1126:
				this.lbTip1.Visible = true;
				this.lbTip2.Visible = true;
				return;
			case 1127:
				this.lbTip1.Visible = false;
				this.lbTip2.Visible = false;
				return;
			default:
				base.DefWndProc(ref m);
				break;
			}
		}

		private void tbLength_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			if (!Common.IsValidating(this.tbLength.Text, 0.0, 10000.0))
			{
				YJKMessageBox.Show("合法范围在1000mm～10000mm之间");
				this.tbLength.Text = this.m_Length;
				return;
			}
			this.m_Length = this.tbLength.Text;
			this.Refresh();
		}

		private void tbHeight_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			if (!Common.IsValidating(this.tbHeight.Text, 0.0, 5000.0))
			{
				YJKMessageBox.Show("合法范围在50mm～50000mm之间");
				this.tbHeight.Text = this.m_Height;
				return;
			}
			this.m_Height = this.tbHeight.Text;
			this.Refresh();
		}

		private void tbOffsetWidth_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			if (!Common.IsValidating(this.tbOffsetWidth.Text, 0.0, 10000.0))
			{
				YJKMessageBox.Show("合法范围在0mm～10000mm之间");
				this.tbOffsetWidth.Text = this.m_OffsetWidth;
				return;
			}
			this.m_OffsetWidth = this.tbOffsetWidth.Text;
			this.Refresh();
		}

		private void tbBottomOffsetWidth_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			if (!Common.IsValidating(this.tbBottomOffsetWidth.Text, 0.0, 10000.0))
			{
				YJKMessageBox.Show("合法范围在0mm～10000mm之间");
				this.tbBottomOffsetWidth.Text = this.m_BottomOffsetWidth;
				return;
			}
			this.m_BottomOffsetWidth = this.tbBottomOffsetWidth.Text;
			this.Refresh();
		}

		private void tbVOffset_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			if (!Common.IsValidating(this.tbVOffset.Text, 0.0, 10000.0))
			{
				YJKMessageBox.Show("合法范围在0mm～10000mm之间");
				this.tbVOffset.Text = this.m_VOffset;
				return;
			}
			this.m_VOffset = this.tbVOffset.Text;
			this.Refresh();
		}

		private void tbHOffset_TextChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			if (!Common.IsValidating(this.tbHOffset.Text, 0.0, 10000.0))
			{
				YJKMessageBox.Show("合法范围在0mm～10000mm之间");
				this.tbHOffset.Text = this.m_HOffset;
				return;
			}
			this.m_HOffset = this.tbHOffset.Text;
			this.Refresh();
		}

		private void cmbSkidResistanceType_TextChanged(object sender, EventArgs e)
		{
			this.Refresh();
		}

		private void rbPerson_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbPerson.Checked)
			{
				this.lbBottomWidth.Enabled = false;
				this.lbOffset.Text = "距门宽度";
				this.tbBottomOffsetWidth.Enabled = false;
			}
			else
			{
				this.lbBottomWidth.Enabled = true;
				this.lbOffset.Text = "顶距门宽";
				this.tbBottomOffsetWidth.Enabled = true;
			}
			if (!this.m_Initialized)
			{
				return;
			}
			this.Refresh();
		}

		private void rbCar_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.rbCar.Checked)
			{
				this.lbBottomWidth.Enabled = false;
				this.lbOffset.Text = "距门宽度";
				this.tbBottomOffsetWidth.Enabled = false;
			}
			else
			{
				this.lbBottomWidth.Enabled = true;
				this.lbOffset.Text = "顶距门宽";
				this.tbBottomOffsetWidth.Enabled = true;
			}
			if (!this.m_Initialized)
			{
				return;
			}
			this.Refresh();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "PassagewayRampForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_Length = "2000";
				this.tbLength.Text = this.m_Length;
				this.m_Height = "500";
				this.tbHeight.Text = this.m_Height;
				this.m_OffsetWidth = "50";
				this.tbOffsetWidth.Text = this.m_OffsetWidth;
				this.m_BottomOffsetWidth = "1000";
				this.tbBottomOffsetWidth.Text = this.m_BottomOffsetWidth;
				this.m_VOffset = "0";
				this.tbVOffset.Text = this.m_VOffset;
				this.m_HOffset = "0";
				this.tbHOffset.Text = this.m_HOffset;
				this.cmbSkidResistanceType.SelectedIndex = 0;
				this.rbPerson.Checked = true;
				this.rbCar.Checked = !this.rbPerson.Checked;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.m_Length = streamReader.ReadLine();
			this.tbLength.Text = this.m_Length;
			this.m_Height = streamReader.ReadLine();
			this.tbHeight.Text = this.m_Height;
			this.m_OffsetWidth = streamReader.ReadLine();
			this.tbOffsetWidth.Text = this.m_OffsetWidth;
			this.cmbSkidResistanceType.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			try
			{
				this.m_VOffset = streamReader.ReadLine();
				this.tbVOffset.Text = this.m_VOffset;
				this.m_HOffset = streamReader.ReadLine();
				this.tbHOffset.Text = this.m_HOffset;
				this.m_BottomOffsetWidth = streamReader.ReadLine();
				if (this.m_BottomOffsetWidth.Trim().CompareTo("") == 0)
				{
					this.m_BottomOffsetWidth = "1000";
				}
				this.tbBottomOffsetWidth.Text = this.m_BottomOffsetWidth;
				this.rbPerson.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbCar.Checked = !this.rbPerson.Checked;
			}
			catch (Exception)
			{
				this.m_VOffset = "0";
				this.tbVOffset.Text = this.m_VOffset;
				this.m_HOffset = "0";
				this.tbHOffset.Text = this.m_HOffset;
				this.m_BottomOffsetWidth = "1000";
				this.tbBottomOffsetWidth.Text = this.m_BottomOffsetWidth;
				this.rbPerson.Checked = true;
				this.rbCar.Checked = !this.rbPerson.Checked;
				streamReader.Close();
				fileStream.Close();
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "PassagewayRampForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_Length);
			streamWriter.WriteLine(this.m_Height);
			streamWriter.WriteLine(this.m_OffsetWidth);
			streamWriter.WriteLine(this.cmbSkidResistanceType.SelectedIndex);
			streamWriter.WriteLine(this.m_VOffset);
			streamWriter.WriteLine(this.m_HOffset);
			streamWriter.WriteLine(this.m_BottomOffsetWidth);
			streamWriter.WriteLine(this.rbPerson.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void PassagewayRampForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			PassagewayRampForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			PassagewayRampForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void PassagewayRampForm_MouseLeave(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbLength.Text, 1000.0, 10000.0))
			{
				this.m_Length = "1000";
				this.tbLength.Text = this.m_Length;
			}
			if (!Common.IsValidating(this.tbOffsetWidth.Text, 0.0, 10000.0))
			{
				this.m_OffsetWidth = "0";
				this.tbOffsetWidth.Text = this.m_OffsetWidth;
			}
			if (!Common.IsValidating(this.tbBottomOffsetWidth.Text, 0.0, 10000.0))
			{
				this.m_BottomOffsetWidth = "0";
				this.tbBottomOffsetWidth.Text = this.m_BottomOffsetWidth;
			}
			if (!Common.IsValidating(this.tbVOffset.Text, 0.0, 10000.0))
			{
				this.m_VOffset = "0";
				this.tbVOffset.Text = this.m_VOffset;
			}
			if (!Common.IsValidating(this.tbHOffset.Text, 0.0, 10000.0))
			{
				this.m_HOffset = "0";
				this.tbHOffset.Text = this.m_HOffset;
			}
			if (!Common.IsValidating(this.tbHeight.Text, 50.0, 5000.0))
			{
				this.m_Height = "50";
				this.tbHeight.Text = this.m_Height;
			}
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				PassagewayRampForm.SendMessage(PassagewayRampDesigner._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private string m_Length = "2000";

		private string m_Height = "500";

		private string m_OffsetWidth = "50";

		private string m_BottomOffsetWidth = "1000";

		private string m_VOffset = "0";

		private string m_HOffset = "0";

		private bool m_Initialized;

		private Previewer m_Previewer;

		private Transform m_Transform;

		private RampPlotter m_Plotter;

		private DoorGeometryExtractor m_Extractor;

		private double m_WallThickness;

		private List<List<EdgeInfo>> m_DoorFaces = new List<List<EdgeInfo>>();

		private List<bool> m_DoorFacesIsGlass = new List<bool>();
	}
}
